home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / DR / DR_UNIX.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  4KB  |  229 lines

  1. /*************************************************************************
  2.  
  3.     dr_unix.c       Directory (dr) subroutines
  4.             for Bywater Software.
  5.  
  6.             Implementation for generic (?) Unix
  7.  
  8.             Copyright (c) 1991, Ted A. Campbell
  9.  
  10.             Bywater Software
  11.             P. O. Box 4023 
  12.             Duke Station 
  13.             Durham, NC  27706
  14.  
  15.             email: tcamp@hercules.acpub.duke.edu
  16.  
  17.     Copyright and Permissions Information:
  18.  
  19.     All U.S. and international copyrights are claimed by the
  20.     author. The author grants permission to use this code
  21.     and software based on it under the following conditions:
  22.     (a) in general, the code and software based upon it may be 
  23.     used by individuals and by non-profit organizations; (b) it
  24.     may also be utilized by governmental agencies in any country,
  25.     with the exception of military agencies; (c) the code and/or
  26.     software based upon it may not be sold for a profit without
  27.     an explicit and specific permission from the author, except
  28.     that a minimal fee may be charged for media on which it is
  29.     copied, and for copying and handling; (d) the code must be 
  30.     distributed in the form in which it has been released by the
  31.     author; and (e) the code and software based upon it may not 
  32.     be used for illegal activities. 
  33.  
  34. **************************************************************************/
  35.  
  36. #include "stdio.h"
  37. #include "string.h"
  38. #include "time.h"
  39. #include "sys/types.h"
  40. #include "sys/stat.h"
  41.  
  42. #include "dr.h"
  43.  
  44. #define    INFILENAME    "."
  45. #define    OUTFILENAME    "/tmp/dr_unix.tmp"
  46.  
  47. #ifndef    TRUE
  48. #define    TRUE    1
  49. #define    FALSE    0
  50. #endif
  51.  
  52. #ifndef    CR
  53. #define    CR    13
  54. #define    LF    10
  55. #endif
  56.  
  57. int    dr_fs = '/';
  58. char    dr_all[ MAX_PATHLENGTH ] = "";
  59.  
  60. extern char *tmpname();
  61.  
  62. char unix_flpath[ MAX_PATHLENGTH ];
  63. char command_string[80];
  64. FILE *unix_fp;
  65. static char      _path[ MAX_PATHLENGTH ];
  66.  
  67. dr_first( findb, retb )
  68.     char *findb; 
  69.     struct dir_ent *retb;
  70.     {
  71.     register int c;
  72.     char *name;
  73.  
  74.     sprintf( command_string, "ls -a -1 %s >%s", findb, OUTFILENAME );
  75.     system( command_string );
  76.  
  77.     if ( ( unix_fp = fopen( OUTFILENAME, "r" )) == NULL) 
  78.         {
  79. #ifdef    DEBUG
  80.         bw_error( "Cannot open temp file"); 
  81. #endif
  82.         return FALSE;
  83.         }
  84.  
  85.     if ( feof( unix_fp ))
  86.         {
  87.         fclose( unix_fp );
  88. #ifdef    DEBUG
  89.         bw_error( "Temporary file is empty." );
  90. #else
  91.         unlink( OUTFILENAME ); 
  92. #endif
  93.         return FALSE;
  94.         }
  95.     else
  96.         {
  97.  
  98.         strcpy( _path, findb );
  99.         c = strlen( _path );
  100.  
  101.         while (( c >= 0 ) && ( _path[ c ] != dr_fs ))
  102.             {
  103. #ifdef  OLD_DEBUG
  104.             fprintf( stderr, "_path: %s c: %d\n", _path,
  105.                 c );
  106. #endif
  107.             _path[ c ] = 0;
  108.             --c;
  109.             }
  110.  
  111.         strcpy( retb->pathname, _path );
  112.  
  113.         name = fgets( command_string, 80, unix_fp );
  114.         if ( unix_fillbuffer( retb, name ) == FALSE )
  115.             {
  116.             return FALSE;
  117.             }
  118.         else
  119.             {
  120.             return TRUE;
  121.             }
  122.         }
  123.     }
  124.  
  125. dr_next( retb )
  126.     struct dir_ent *retb;
  127.     {
  128.     char *name;
  129.  
  130.     if ( feof( unix_fp ) )
  131.         {
  132.         fclose( unix_fp );
  133. #ifndef    DEBUG
  134.         unlink( OUTFILENAME ); 
  135. #endif
  136.         return FALSE;
  137.         }
  138.     else
  139.         {
  140.         strcpy( retb->pathname, _path );
  141.         name = fgets( command_string, 80, unix_fp );
  142.         if ( unix_fillbuffer( retb, name ) == FALSE )
  143.             {
  144.             return FALSE;
  145.             }
  146.         else
  147.             {
  148.             return TRUE;
  149.             }
  150.         }
  151.     }
  152.  
  153. unix_fillbuffer( retb, name )
  154.     struct dir_ent *retb;
  155.     char *name;
  156.     {
  157.     static struct stat s_buf;
  158.  
  159. #ifdef    OLD_DEBUG
  160.     fprintf( stderr, "unix_fillbuffer() received filename: <%s> \n", name );
  161. #endif
  162.  
  163.     if ( name == NULL )
  164.         {
  165.         return FALSE;
  166.         }
  167.  
  168.     strcpy( retb->filename, name );
  169.     unix_truncate( retb->filename );
  170.  
  171.     if ( strlen( retb->filename ) == 0 )
  172.         {
  173. #ifdef    DEBUG
  174.         bw_error( "Null filename returned." );
  175. #endif
  176.         return FALSE;
  177.         }
  178.  
  179.     if ( stat( retb->filename, &s_buf ) != 0 )
  180.         {
  181. #ifdef    OLD_DEBUG
  182.         fprintf( stderr, "Can't stat file %s\n", retb->filename );
  183. #endif
  184.         }
  185.  
  186.     retb->size = s_buf.st_size;
  187.  
  188.     if ( ( s_buf.st_mode & 0040000 ) == 0040000 )
  189.         {
  190.         retb->type = FILE_DIRECTORY;
  191.         }
  192.     else if ( ( s_buf.st_mode & 0000400 ) == 0000007 )
  193.         {
  194.         retb->type = FILE_EXECUTABLE;
  195.         }
  196.     else if ( ( s_buf.st_mode & 0000200 ) == 0000070 )
  197.         {
  198.         retb->type = FILE_EXECUTABLE;
  199.         }
  200.     else if ( ( s_buf.st_mode & 0000100 ) == 0000100 )
  201.         {
  202.         retb->type = FILE_EXECUTABLE;
  203.         }
  204.     else
  205.         {
  206.         retb->type = FILE_NORMAL;
  207.         }
  208.  
  209.     return TRUE;
  210.  
  211.     }
  212.  
  213. unix_truncate( buffer )
  214.     char *buffer;
  215.     {
  216.     register char *p;
  217.  
  218.     p = buffer;
  219.     while( *p != 0 )
  220.         {
  221.         if (( *p == CR ) || ( *p == LF ))
  222.             {
  223.             *p = 0;
  224.             }
  225.         ++p;
  226.         }
  227.     }
  228.  
  229.